﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using nanoCMS.Core;
using nanoCMS.Forum.WebUI.Models;
using nanoCMS.Forum.WebUI.Models.AdminViewModels;
using nanoCMS.Forum.WebUI.Models.ViewModels;

namespace nanoCMS.Forum.WebUI.Controllers
{
	public class ForumAdminController : Controller
	{
		private readonly WorldContext worldContext;
		private ForumAdminService forumService;

		public ForumAdminController(WorldContext worldContext)
		{
			this.worldContext = worldContext;
			this.forumService = new ForumAdminService(worldContext);
		}

		public ActionResult Index(int forumID = 0)
		{
            if (forumID == 0)
            {
                return PartialView("Index", forumService.CreateForumAdminModel());
            }
            else
            {
                return PartialView("Index", forumService.EditForumAdminModel(forumID));
            }
		}

		public ActionResult Save(ForumEntryVM forumEntryVM)
		{
			if(ModelState.IsValid)
			{
				if (forumEntryVM.ForumID == 0)
				{
					forumService.CreateNewForum(forumEntryVM);
				} 
				else
				{
					forumService.EditNewForum(forumEntryVM);
				}
			}
            return PartialView("AdminForm", forumService.CreateForumAdminModel()); 
		}

		public ActionResult Edit(int forumID)
		{
            return PartialView("AdminForm", forumService.EditForumAdminModel(forumID));
		}

		public ActionResult Reorder(int forumID, bool up)
		{
			if (ModelState.IsValid)
			{
				forumService.ChangeForumOrder(forumID, up);
			}
			return PartialView("AdminForm", forumService.CreateForumAdminModel()); 
		}

        [HttpGet]
        public ActionResult MoveThread(int threadID)
        {
            return PartialView("MoveThreadIndex", forumService.CreateMoveThreadModel(threadID));
        }

        [HttpPost]
        public ActionResult MoveThreadSave(ThreadVM thread)
        {
            forumService.MoveThread(thread);
            return PartialView("MoveThreadForm", forumService.CreateMoveThreadModel(thread.ThreadID));
        }
	}
}